
#import "XMTabBarItemView.h"


@interface XMTabBarItemView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *badgeLabel;
@property (nonatomic, assign) YHTabBarItemViewsStyle currentsStyle;
//@property (nonatomic, strong) LOTAnimationView *lottieView;
@end


@implementation XMTabBarItemView

- (instancetype)init {
    if (self = [super init]) {
        self.currentsStyle = YHTabBarItemViewsStyleDefault;
        [self reloadSubViews];
        [self addTapGestureRecogniser];
    }
    return self;
}

- (instancetype)initWithStyle:(YHTabBarItemViewsStyle)style {
    if (self = [super init]) {
        self.currentsStyle = style;
        [self reloadSubViews];
        [self addTapGestureRecogniser];
    }
    return self;
}

- (void)reloadSubViews {
    switch (self.currentsStyle) {
        case YHTabBarItemViewsStyleDefault: {
            [self addSubview:self.titleLabel];
            [self addSubview:self.imageView];
            [self addSubview:self.badgeLabel];
            
            [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self).offset(9.f);
                make.centerX.equalTo(self.titleLabel);
                make.width.mas_equalTo(22.f);
                make.height.mas_equalTo(22.f);
            }];
            
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.right.equalTo(self);
                make.top.equalTo(self.imageView.mas_bottom).offset(2.5f);
            }];
            
            [self.badgeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.imageView).offset(-8);
                make.centerX.equalTo(self.imageView.mas_right);
                make.height.mas_equalTo(16);
                make.width.mas_equalTo(16);
            }];
            
        } break;

        case YHTabBarItemViewsStyleCircleTitle: {
            [self addSubview:self.imageView];
            [self.titleLabel removeFromSuperview];
            [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.center.equalTo(self);
                make.width.height.mas_equalTo(42.f);
            }];
        } break;

        default:
            break;
    }
    [self layoutIfNeeded];
}

- (void)addTapGestureRecogniser {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCurrentView)];
    [self addGestureRecognizer:tapGesture];
}

- (void)clickCurrentView {
    if (self.clickBlock) {
        self.clickBlock();
    }
//    if (![NSString isEmpty:self.clickLottieJson]) {
//        if (self.lottieView) {
//            if (self.lottieView.isAnimationPlaying) {
//                [self.lottieView stop];
//            }
//            [self.lottieView removeFromSuperview];
//        } else {
//            self.lottieView = [LOTAnimationView animationNamed:self.clickLottieJson];
//        }
//
//        [self addSubview:self.lottieView];
//        [self.lottieView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.center.equalTo(self.imageView);
//            make.width.mas_equalTo(22.f);
//            make.height.mas_equalTo(19.f);
//            make.size.mas_equalTo(CGSizeMake(20.f, 20.f));
//        }];
//        self.lottieView.loopAnimation = NO;
//        WeakSelf;
//        self.lottieView.completionBlock = ^(BOOL animationFinished) {
//            [weakSelf.lottieView removeFromSuperview];
//        };
//        [self.lottieView play];
//    }
}

#pragma mark - Setter
- (void)setCurrentsStyle:(YHTabBarItemViewsStyle)currentsStyle {
    YHTabBarItemViewsStyle lastStyle = _currentsStyle;
    _currentsStyle = currentsStyle;
    if (lastStyle != currentsStyle) {
        [self reloadSubViews];
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (void)setTitleFont:(UIFont *)titleFont {
    _titleFont = titleFont;
    self.titleLabel.font = titleFont;
}

- (void)setTitleColor:(UIColor *)titleColor {
    _titleColor = titleColor;
    if (!self.selected) {
        self.titleLabel.textColor = titleColor;
    }
}

- (void)setTitleSelectedColor:(UIColor *)titleSelectedColor {
    _titleSelectedColor = titleSelectedColor;
    if (self.selected) {
        self.titleLabel.textColor = titleSelectedColor;
    }
}

- (void)setImage:(UIImage *)image {
    _image = image;
    if (!self.selected) {
        self.imageView.image = image;
    }
}

- (void)setImageSize:(CGSize)imageSize {
    _imageSize = imageSize;
    if (imageSize.width > 0.f && imageSize.height > 0.f) {
        [self.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(imageSize.width);
            make.height.mas_equalTo(imageSize.height);
        }];
    }
}

- (void)setImageSelected:(UIImage *)imageSelected {
    _imageSelected = imageSelected;
    if (self.selected) {
        self.imageView.image = imageSelected;
    }
}

- (void)setBadgeNum:(NSInteger)badgeNum {
    _badgeNum = badgeNum;
    self.badgeLabel.hidden = badgeNum <= 0;
    NSString *badgeStr = @"";
    if (badgeNum < 100 && badgeNum > 0) {
        badgeStr = [NSString stringWithFormat:@"%ld", badgeNum];
    } else if (badgeNum >= 100 && badgeNum < 1000) {
        badgeStr = @"99+";
    } else if (badgeNum >= 1000) {
        badgeStr = @"⋯";
    } else {
        badgeStr = @"";
        [self.badgeLabel setHidden:YES];
    }
    self.badgeLabel.text = badgeStr;
//    CGFloat badgeW = [badgeStr xm_textSizeIn:CGSizeMake(CGFLOAT_MAX, 16) font:[UIFont systemFontOfSize:10]].width + 8;
    
    CGFloat badgeW = [badgeStr widthForFont:kUIFont(10)] + 8;
    badgeW = MAX(16, badgeW);
    if (badgeNum < 10) {
        badgeW = 16;
    }
    [self.badgeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(badgeW);
    }];
}

- (void)setSelected:(BOOL)selected {
    _selected = selected;
    if (selected) {
        if (self.titleSelectedColor) {
            self.titleLabel.textColor = self.titleSelectedColor;
        }
        if (self.imageSelected) {
            self.imageView.image = self.imageSelected;
        }
    } else {
        if (self.titleColor) {
            self.titleLabel.textColor = self.titleColor;
        }
        if (self.image) {
            self.imageView.image = self.image;
        }
    }
}

#pragma mark - Getter
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:10];
    }
    return _titleLabel;
}

- (UILabel *)badgeLabel {
    if (!_badgeLabel) {
        _badgeLabel = [[UILabel alloc] init];
        _badgeLabel.textAlignment = NSTextAlignmentCenter;
        _badgeLabel.font = [UIFont systemFontOfSize:10];
        _badgeLabel.textColor = UIColor.whiteColor;
        _badgeLabel.hidden = YES;
        _badgeLabel.backgroundColor = UIColor.redColor;
        [_badgeLabel.layer setCornerRadius:8];
        [_badgeLabel.layer setMasksToBounds:YES];
        [_badgeLabel.layer setBorderWidth:0];
        [_badgeLabel.layer setBorderColor:[UIColor clearColor].CGColor];
    }
    return _badgeLabel;
}

@end
